<?php defined('In33hao') or exit('Access Invalid!'); ?>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <div class="subject">
                <h3><?php echo $lang['diymenu']; ?></h3>
                <h5><?php echo $lang['diymenu_subhead']; ?></h5>
            </div>
            <?php echo $output['top_link']; ?> </div>
    </div>
    <!-- 操作说明 -->
    <div class="explanation" id="explanation">
        <div class="title" id="checkZoom"><i class="fa fa-lightbulb-o"></i>
            <h4 title="<?php echo $lang['nc_prompts_title']; ?>"><?php echo $lang['nc_prompts']; ?></h4>
            <span id="explanationZoom" title="<?php echo $lang['nc_prompts_span']; ?>"></span></div>
        <ul>
            <li><?php echo $lang['diymenu_subhead']; ?></li>
        </ul>
    </div>
    <form method='post'>
        <input type="hidden" name="form_submit" value="ok"/>
        <input type="hidden" name="submit_type" id="submit_type" value=""/>
        <table class="flex-table">
            <thead>
            <tr>
                <th width="24" align="center" class="sign"><i class="ico-check"></i></th>
                <th width="150" class="handle" align="center"><?php echo $lang['nc_handle']; ?></th>
                <th width="150" align="left">分类名称</th>
                <th width="80" align="center">显示</th>
                <th width="60" align="center"><?php echo $lang['nc_sort']; ?></th>
                <th width="600" align="left">url链接</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            <?php if (!empty($output['wx_class']) && is_array($output['wx_class'])) { ?>
                <?php foreach ($output['wx_class'] as $k => $v) { ?>
                    <tr data-id="<?php echo $v['id']; ?>">
                        <td class="sign"><i class="ico-check"></i></td>
                        <td class="handle">
                            <a class="btn blue" href="index.php?act=hao&op=diymenu_edit&id=<?php echo $v['id']?>"><i
                                    class="fa fa-pencil-square-o"></i><?php echo $lang['nc_edit']; ?></a>
                            <a class="btn red" href="javascript:void(0);" onclick="fg_del(<?php echo $v['id']; ?>);"><i
                                    class="fa fa-trash-o"></i><?php echo $lang['nc_del']; ?></a>
                        </td>
                        <td class="name">
                            <?php echo $v['title']; ?>
                            <?php if ($v['deep'] == 1) { ?>
                                <a class="btn" href="javascript:void(0);" onclick="fg_add(<?php echo $v['id']; ?>,0);">新增下级</a>
                            <?php } ?>
                        </td>
                        <td><?php if ($v['is_show'] == 1) { ?>
                                显示
                            <?php } else { ?>
                                隐藏
                            <?php } ?></td>
                        <td class="sort"><span title="<?php echo $lang['nc_editable']; ?>"
                                               column_id="<?php echo $v['id']; ?>" fieldname="sort"
                                               nc_type="inline_edit" class="editable "><?php echo $v['sort']; ?></span>
                        </td>
                        <td><?php echo $v['url']; ?></td>
                        <td></td>
                    </tr>
                <?php } ?>
            <?php } else { ?>
                <tr>
                    <td class="no-data" colspan="100"><i
                            class="fa fa-exclamation-circle"></i><?php echo $lang['nc_no_record']; ?></td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
    </form>
</div>
<script type="text/javascript" src="<?php echo ADMIN_RESOURCE_URL; ?>/js/jquery.edit.js" charset="utf-8"></script>
<script type="text/javascript">
    $(function () {
        $('.flex-table').flexigrid({
            height: 'auto',// 高度自动
            usepager: false,// 不翻页
            striped: false,// 不使用斑马线
            resizable: false,// 不调节大小
            title: '微信菜单(一级)',// 表格标题
            reload: false,// 不使用刷新
            columnControl: false,// 不使用列控制
            buttons: [
                {display: '<i class="fa fa-plus"></i>添加菜单', name: 'add', bclass: 'add', onpress: fg_operation},
                {
                    display: '<i class="fa fa-trash"></i>批量删除',
                    name: 'del',
                    bclass: 'del',
                    title: '将选定行数据批量删除',
                    onpress: fg_operation
                },
                {
                    display: '<i class="fa fa-file-excel-o"></i>生成菜单',
                    name: 'csv',
                    bclass: 'csv',
                    title: '生成微信自定义菜单',
                    onpress: fg_operation
                }
            ]
        });

        $('span[nc_type="inline_edit"]').inline_edit({act: 'hao', op: 'diymenu_sort_ajax'});
    });

    function fg_operation(name, bDiv) {
        if (name == 'add') {
            fg_add(0,1);
        } else if (name == 'del') {
            if ($('.trSelected', bDiv).length == 0) {
                showError('请选择要操作的数据项！');
            }
            var itemids = new Array();
            $('.trSelected', bDiv).each(function (i) {
                itemids[i] = $(this).attr('data-id');
            });
            fg_del(itemids);
        } else if (name = 'csv') {
            window.location.href = 'index.php?act=hao&op=menu_send';
        }
    }
    function fg_del(ids) {
        if (typeof ids == 'number') {
            var ids = new Array(ids.toString());
        }
        id = ids.join(',');
        if (confirm('删除后将不能恢复，确认删除这项吗？')) {
            $.getJSON('index.php?act=hao&op=diymenu_delete', {id: id}, function (data) {
                if (data.status) {
                    location.reload();
                } else {
                    showError('删除失败');
                }
            });
        }
    }
    function fg_add(id,type) {
        $.getJSON('index.php?act=hao&op=check_add_class', {id: id,type:type}, function (data) {
            if (data.status) {
                window.location.href = data.url;
            } else {
                if (type) {
                    showError('顶级菜单只能添加3个')
                } else {
                    showError('每个顶级菜单只能添加5个子菜单')
                }
            }
        });
    }
</script>